<script setup lang="ts">
import type { SelectOption } from 'naive-ui';
import { onMounted } from 'vue';
import { $t } from '@/locales';
import { type SmsLogSearchParams, fetchSmsChannelOptions } from '@/service/api';
import { DICT_TYPE, getDictLabel, getDictOptions } from '@/utils/dict';

defineOptions({
  name: 'SmsLogSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const model = defineModel<SmsLogSearchParams>('model', { required: true });

const channelIdOptions: any = [];

function reset() {
  emit('reset');
}

function search() {
  emit('search');
}

const channelIdRenderLabel = (option: SelectOption) => {
  return `${option.extra}【 ${getDictLabel(DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE, option.label)}】`;
};

onMounted(async () => {
  // 加载渠道列表
  const { data: res } = await fetchSmsChannelOptions();
  res?.forEach(item => {
    channelIdOptions.push({
      label: item.label,
      value: item.value,
      extra: item.extra
    });
  });
});
</script>

<template>
  <NCard :title="$t('common.search')" :bordered="false" size="small" class="card-wrapper">
    <NForm :model="model" label-placement="left" :label-width="80">
      <NGrid responsive="screen" item-responsive>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.mobile')" path="mobile" class="pr-24px">
          <NInput v-model:value="model.mobile" clearable :placeholder="$t('page.base.sms.log.form.mobile')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.channelId')" path="channelId" class="pr-24px">
          <NSelect
            v-model:value="model.channelId"
            :placeholder="$t('page.base.sms.log.form.channelId')"
            :options="channelIdOptions"
            :render-label="channelIdRenderLabel"
            clearable
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.templateId')" path="templateId" class="pr-24px">
          <NInput v-model:value="model.templateId" clearable :placeholder="$t('page.base.sms.log.form.templateId')" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.sendStatus')" path="sendStatus" class="pr-24px">
          <NSelect
            v-model:value="model.sendStatus"
            :placeholder="$t('page.base.sms.log.form.sendStatus')"
            :options="getDictOptions(DICT_TYPE.COMMON_STATUS)"
            clearable
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.sendTime')" path="sendTime" class="pr-24px">
          <DataPicker v-model:value="model.sendTime" type="datetimerange" :is-picker-options="true" />
        </NFormItemGi>
        <NFormItemGi
          span="24 s:12 m:6"
          :label="$t('page.base.sms.log.receiveStatus')"
          path="receiveStatus"
          class="pr-24px"
        >
          <NSelect
            v-model:value="model.receiveStatus"
            :placeholder="$t('page.base.sms.log.form.receiveStatus')"
            :options="getDictOptions(DICT_TYPE.COMMON_STATUS)"
            clearable
          />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6" :label="$t('page.base.sms.log.receiveTime')" path="receiveTime" class="pr-24px">
          <DataPicker v-model:value="model.receiveTime" type="datetimerange" :is-picker-options="true" />
        </NFormItemGi>
        <NFormItemGi span="24 s:12 m:6">
          <NSpace class="w-full" justify="end">
            <NButton @click="reset">
              <template #icon>
                <icon-ic-round-refresh class="text-icon" />
              </template>
              {{ $t('common.reset') }}
            </NButton>
            <NButton type="primary" ghost @click="search">
              <template #icon>
                <icon-ic-round-search class="text-icon" />
              </template>
              {{ $t('common.search') }}
            </NButton>
          </NSpace>
        </NFormItemGi>
      </NGrid>
    </NForm>
  </NCard>
</template>

<style scoped></style>
